1 // Fig. 7.3: fig07_03.cpp 2 // Attempting to initialize a constant of 3 // a built-in data type with an assignment. 4 #include 5 6 class Increment { 7 public: 8 Increment( int c = 0, int i = 1 ); 9 void addIncrement() { count += increment; } 10 void print() const; 11 private: 12 int count; 13 const int increment; 14 }; 15 16 // Constructor for class Increment 17 Increment::Increment( int c, int i ) 18 { // Constant member 'increment' is not initialized 19 count = c; 20 increment = i; // ERROR: Cannot modify a const object 21 } 22 23 // Print the data 24 void Increment::print() const 25 { 26 cout << "count = " << count 27 << ", increment = " << increment << endl; 28 } 29 30 int main() 31 { 32 Increment value( 10, 5 ); 33 34 cout << "Before incrementing: "; 35 value.print(); 36 37 for ( int j = 0; j < 3; j++ ) { 38 value.addIncrement(); 39 cout << "After increment " << j << ": "; 40 value.print(); 41 } 42 43 return 0; 44 }